<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
      <el-form-item label="登录地址" prop="ipaddr">
        <el-input v-model="queryParams.ipaddr" placeholder="请输入登录地址" clearable style="width: 240px" size="small" @keyup.native.native="handleQuery"></el-input>
      </el-form-item>
    <el-form-item label="用户名称" prop="userName">
      <el-input v-model="queryParams.userName" placeholder="请输入用户名称" clearable style="width: 240px" size="small" @keyup.enter.native="handleQuery">
      </el-input>
    </el-form-item>
    <el-form-item label="状态" prop="status">
      <el-select v-model="queryParams.status" placeholder="登录状态" clearable size="small" style="width: 240px">
        <el-option v-for="dict in statusOptions" :key="dict.key" :label="dict.value" :value="dict.key"></el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="登录时间">
      <el-date-picker v-model="dateRange" size="small" style="width: 240px" value-format="yyyy-MM-dd" type="daterange" range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
      <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">搜索</el-button>
    </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="danger" icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleClean">清空</el-button>
      </el-col>
    </el-row>
    <el-table v-loading="loading" :data="list" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="编号" prop="infoId" align="center"/>
      <el-table-column label="登录名称" prop="loginName" align="center"/>
      <el-table-column label="登录地址" prop="ipaddr" align="center" width="130" :show-overflow-tooltip="true"/>
      <el-table-column label="登录地点" prop="loginLocation" align="center" :show-overflow-tooltip="true"/>
      <el-table-column label="浏览器" prop="browser" align="center"/>
      <el-table-column label="操作系统" prop="os" align="center"/>
      <el-table-column label="登录状态" prop="status" align="center" :formatter="statusFormat" />
      <el-table-column label="操作信息" prop="msg" align="center"/>
      <el-table-column label="登录日期" prop="loginTime" align="center" width="180">
        <template slot-scope="scope">
          <span>{{moment(parseInt(scope.row.loginTime) * 1000).format('YYYY-MM-DD HH:mm:ss')}}</span>
        </template>
      </el-table-column>
      <el-table-column label="登录模块" align="center" prop="module"></el-table-column>
    </el-table>
    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList"></pagination>
  </div>
</template>

<script>
import {delLoginLog, list} from '../../../../api/system/monitor/logininfo'

export default {
  name: 'index',
  data () {
    return {
      // 遮罩层
      loading: true,
      ids: [],
      // 非多个禁用
      multiple: true,
      total: 0,
      list: [],
      statusOptions: [],
      dateRange: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        ipaddr: undefined,
        userName: undefined,
        status: undefined
      }
    }
  },
  created () {
    this.getList()
  },
  methods: {
    // 查询登录日志列表
    getList () {
      this.loading = true
      list(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
        this.statusOptions = response.data.searchStatus.values || []
        this.list = response.data.list
        this.total = response.data.total
        this.loading = false
      })
    },
    // 搜索按钮操作
    handleQuery () {
      this.queryParams.pageNum = 1
      this.getList()
    },
    resetQuery () {
      this.dateRange = []
      this.resetForm('queryForm')
      this.handleQuery()
    },
    handleDelete () {
      const infoIds = this.ids
      this.$confirm('是否确认删除访问编号为"' + infoIds + '"的数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function () {
        return delLoginLog(infoIds)
      }).then(response => {
        if (response.data) {
          this.getList()
          this.msgSuccess('删除成功')
        } else {
          this.getList()
          this.msgError('删除失败')
        }
      })
    },
    handleClean () {
      console.log()
    },
    handleSelectionChange (selection) {
      this.ids = selection.map(item => item.infoId)
      this.multiple = !selection.length
    },
    statusFormat () {
      console.log()
    }
  }
}
</script>

<style scoped>

</style>
